﻿using AspNetCoreRateLimit;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Threading.Tasks;

namespace housingsafety.server.Helpers
{
    /// <summary>
    /// 通常在项目中，Authorization授权是少不了，加入限流后，在被限流的接口调用后
    /// ，限流拦截器使得跨域策略失效，故重写拦截器中间件，继承IpRateLimitMiddleware 即可
    /// </summary>
    public class IpLimitMiddleware : IpRateLimitMiddleware
    {
        public IpLimitMiddleware(RequestDelegate next, IOptions<IpRateLimitOptions> options, IRateLimitCounterStore counterStore
            , IIpPolicyStore policyStore, IRateLimitConfiguration config, ILogger<IpRateLimitMiddleware> logger)
            : base(next, options, counterStore, policyStore, config, logger)
        {
        }

        public override Task ReturnQuotaExceededResponse(HttpContext httpContext, RateLimitRule rule, string retryAfter)
        {
            httpContext.Response.Headers.Append("Access-Control-Allow-Origin", "*");
            return base.ReturnQuotaExceededResponse(httpContext, rule, retryAfter);
        }
    }
}
